package co.fitstart.api.handler.impl;

import java.math.BigDecimal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.tempurl.EsalesLocator;
import org.tempurl.EsalesSoap;
import org.tempurl.EsalesSoapStub;
import org.tempurl.Esaleshdr;
import org.tempurl.Esalesitem;
import org.tempurl.Esalestender;
import org.tempurl.Postesalescreaterequest;
import org.tempurl.Postesalescreateresponse;
import org.tempurl.Requestheader;
import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.web.support.HttpResult;
import co.fitstart.api.handler.WxcClientOrderHandler;
import co.fitstart.api.webservice.WxcAccount;
import co.fitstart.api.webservice.WxcPayType;
import co.fitstart.entity.order.Order;


/**
 * 
 * @author mohanlan
 *
 */
@Component("wxcClientOrderHandler")
public class WxcClientOrderHandlerImpl implements WxcClientOrderHandler {
    
    private static final Logger logger = LoggerFactory.getLogger(WxcClientOrderHandler.class);
    /*
     * (non-Javadoc)
     * @see co.fitstart.api.handler.WxcClientOrderHandler#create(co.fitstart.entity.order.Order)
     */
    public HttpResult create(Order order) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            EsalesLocator locator = new EsalesLocator();
            
            EsalesSoap esalesSoap = locator.getesalesSoap();
            
            Postesalescreaterequest astrRequest = new Postesalescreaterequest();
            
            // 1. set header
            Requestheader requestHeader = this.generateRequestheader();
            astrRequest.setHeader(requestHeader);
            
            // 2. set esaleshdr
            Esaleshdr esaleshdr = this.generateEsaleshdr(order);
            astrRequest.setEsalestotal(esaleshdr);
            
            // 3. set esalesitems
            Esalesitem []  esalesitems = this.generateArrayOfEsalesitem(order);
            astrRequest.setEsalesitems(esalesitems);
            
            // 4. set esalestenders
            Esalestender[] esalestenders = this.generateArrayOfEsalestender(order);
            astrRequest.setEsalestenders(esalestenders);
            
            // 5. post order
            Postesalescreateresponse result = esalesSoap.postesalescreate(astrRequest);

            if(result.getHeader().getResponsecode() == 0) {
                httpResult.success().message("上传订单数据成功");
            }else {
                httpResult.fail().message(result.getHeader().getResponsemessage());
            }
            
        } catch (Exception e) {
            
            logger.error("#create post order to wxc occurs error", e);
            httpResult.fail().message("上传订单数据失败");
        }
        
        return httpResult;
    }
    
    /**
     * 
     * @return
     */
    private Requestheader generateRequestheader() {
        
        Requestheader requestheader = new Requestheader();
        requestheader.setUsername(WxcAccount.HEADER_USERNAME);
        requestheader.setPassword(WxcAccount.HEADER_PASSWORD);
        requestheader.setPagerecords(WxcAccount.HEADER_PAGE_RECORDS);
        requestheader.setPageno(WxcAccount.HEADER_PAGE_NUMBER);
        requestheader.setUpdatecount(WxcAccount.HEADER_UPDATE_COUNT);
        requestheader.setMessagetype(WxcAccount.HEADER_MESSAGE_TYPE);
        requestheader.setMessageid(WxcAccount.HEADER_MESSAGE_ID);
        requestheader.setVersion(WxcAccount.HEADER_VERSION);
        
        return requestheader;
    }
    
    /**
     * 
     * @param order
     */
    private Esaleshdr generateEsaleshdr(Order order) {
        
        Esaleshdr esaleshdr = new Esaleshdr();
        esaleshdr.setTxdate_yyyymmdd(DateFormatUtils.format(order.getPayTime(), "yyyyMMdd"));
        esaleshdr.setTxtime_hhmmss(DateFormatUtils.format(order.getPayTime(), "HHmmss"));
        esaleshdr.setMallid(WxcAccount.MALL_ID);
        esaleshdr.setStorecode(WxcAccount.STORE_CODE);
        esaleshdr.setTillid(WxcAccount.TILL_ID);
        esaleshdr.setTxdocno(order.getCode());
        esaleshdr.setCashier(WxcAccount.CASHIER);
        esaleshdr.setNetqty(new BigDecimal(order.getAmount()));
        esaleshdr.setNetamount(new BigDecimal(order.getPrice()).divide(new BigDecimal(100)));
        
        return esaleshdr;
    }
    
    /**
     * 
     * @param order
     * @return
     */
    private  Esalesitem [] generateArrayOfEsalesitem(Order order) {

        
        // set esalesitems
        Esalesitem esalesitem = new Esalesitem();
        esalesitem.setLineno(WxcAccount.LINE_NO);
        esalesitem.setItemcode(WxcAccount.SKU_CODE);
        esalesitem.setQty(new BigDecimal(order.getAmount()));
        esalesitem.setNetamount(new BigDecimal(order.getPrice()).divide(new BigDecimal(100)));
        esalesitem.setDiscountamount(new BigDecimal(0));
        esalesitem.setBonusearn(new BigDecimal(0));

        Esalesitem [] esalesitems = {esalesitem};
        
        return esalesitems;
    }
    
    /**
     * 
     * @param order
     * @return
     */
    private Esalestender[] generateArrayOfEsalestender(Order order) {
        
        Esalestender esalestender = new Esalestender();
        esalestender.setLineno(WxcAccount.LINE_NO);
        
        // FIXME order pay type set here
        esalestender.setTendercode(WxcPayType.OT.getAlias());
        
        esalestender.setPayamount(new BigDecimal(order.getPrice()).divide(new BigDecimal(100)));
        esalestender.setBaseamount(new BigDecimal(order.getPrice()).divide(new BigDecimal(100)));
        esalestender.setExcessamount(new BigDecimal(0));


        Esalestender[] esalestenders = {esalestender};
        
        return esalestenders;
    }

}
